<?php
defined('IN_XLP') or exit('Access Denied');

$Document = array(
	'pageid'   => 'goods-spec', //页面标示
	'pagename' => '商品规格', //当前页面名称
	'mycss'	   => array(), //加载的css样式表
	'myjs'	   => array(), //加载的js脚本
	//'footerjs'=>array('content/highcharts', 'content/exporting',),
	'footerjs' => array(),
	'head'	   => true, //加载头部文件
);

include getTpl('header', 'public');
?>
<!--顶部导航 开始-->
<?php include getTpl('top', 'public');?>
<!--顶部导航 结束-->
<!--左侧菜单 开始-->
<?php include getTpl('nav', 'public');?>
<!--左侧菜单 结束-->
<!--主体 开始-->
<!-- content -->
<section id="content">
	<section class="main padder">
		<div class="clearfix">
			<h4><i class="fa fa-table"></i>规格管理</h4>
		</div>
		<div class="row">
			<div class="col-lg-12">
				<section class="panel">

					<header class="panel-heading">
						规格类别
					</header>

					<div class="panel-body">
						<div class="row text-small">
							<div class="col-sm-8 m-b-mini">
								<a href="<?php echo U('goods/specadd'); ?>" class="btn btn-sm btn-white">添加新规格</a>
							</div>
						
							<div class="panel-body">
								<div class="row text-small">
									<form action="">
										<div class="col-sm-4">
											<div class="input-group">
												<input type="text" class="input-sm form-control" placeholder="Search" name="keyword" id="keyword" value="<?php //echo $keyword; ?>"> 
												<span class="input-group-btn"><button class="btn btn-sm btn-white" type="submit">搜索</button></span>
											</div>
										</div>
									</form>
								</div>
							</div>
						</div>
					</div>

					<style>.table th,.table td,.table input{text-align:center;}</style>
					<div class="table-responsive">
						<table class="table table-striped b-t text-small">
							<thead>
								<tr>
									<th>ID</th>
									<th>规格名称</th>
									<th style="width:300px;">规格值</th>
									<th>添加/编辑规格值</th>
									<th>添加/编辑规格备注</th>
									<th>操作</th>
								</tr>
							</thead>
							<tbody>
								<?php if (count($spec)): ?>
								<?php foreach ($spec as $v): ?>
								<tr>
									<td style="line-height:33px;">
										<?php echo $v['id']; ?>
										<input type="hidden" name="spec_id" value="<?php echo $v['id']; ?>">
									</td>
									<td style="line-height:33px;">
										<?php echo $v['spec_name']; ?>
										<input type="hidden" name="spec_name" value="<?php echo $v['spec_name']; ?>">
									</td>
									<td>
										<select name="spec_value_id" class="spec_value form-control">
										<option value="0">选择需要编辑的规格 OR 直接添加</option>
										<?php foreach ($v['spec_value'] as $value): ?>
										<option value="<?php echo $value['id']; ?>" data-value="<?php echo $value['spec_value']; ?>" data-tips="<?php echo $value['spec_tips']; ?>" >
											<?php echo $value['spec_value'].' ('.$value['spec_tips'].')'; ?>
										</option>
										<?php endforeach ?>
										</select>
								    </td>
									<td class="edit_spec">
										<input type="text" name="spec_value" placeholder="添加规格值" class="form-control">
									</td>
									<td class="edit_tips">
										<input type="text" name="spec_tips" placeholder="添加规格备注（可选，30字之内）" class="form-control">
									</td>
									<td><a class="save_spec" style="line-height:33px;" href="javascript:;">保存</a></td>
								</tr>
								<?php endforeach ?>
								<?php else: ?>
								<tr><td colspan="5" align="center">暂无数据</td></tr>
								<?php endif ?>
							</tbody>
						</table>
					</div>

				</section>
			</div>
		</div>
	</section>
</section>
<!--/ content --> 
<!--主体 结束-->
<script>
$(function(){
	$('.spec_value').change(function(){
		var index = $(this).val();
		var edit_spec = $(this).parent().siblings('.edit_spec').children('input');
		var edit_tips = $(this).parent().siblings('.edit_tips').children('input');
		if(index != 0) {
			edit_spec.val($(this).children('option:selected').data('value'));
			edit_tips.val($(this).children('option:selected').data('tips'));
		} else {
			edit_spec.val('');
			edit_tips.val('');
		}
	});

	$('.save_spec').click(function(){
		var $parent = $(this).parent().parent();
		// console.log($parent.get(0));
		var edit_spec = $(this).parent().siblings('.edit_spec').children('input').val();
		if($.trim(edit_spec) == '') {
			Msg.error('规格值不能为空');
			return false;
		}

    	Msg.loading();
		$.post('<?php echo U("goods/spec_value_add") ?>', {
			"spec_id"       : $parent.find('input[name="spec_id"]').val(),
			"spec_name"     : $parent.find('input[name="spec_name"]').val(),
			"spec_value_id" : $parent.find('select option:selected').val(),
			"spec_value"    : $parent.find('input[name="spec_value"]').val(),
			"spec_tips"     : $parent.find('input[name="spec_tips"]').val(),
		}, function(json){
			Msg.hide();
			if(json.err==1) {
				Msg.error(json.msg);
			} else {
				Msg.ok(json.msg);
			}
		}, 'JSON');
		
	});

});
</script>
<?php include getTpl('footer', 'public'); ?>